﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Ccit.Utils;
using Ccit.ClassLibrary;

namespace Ccit.SysPlat.Purview
{
    /// <summary>
    /// Description: 系统平台权限菜单 -> 菜单管理编辑页面
    /// Author: tim 2010-03-02
    /// </summary>
    public partial class sys_menu_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string upMenuId = (null != Request.QueryString["upmenuId"]) ? Request.QueryString["upmenuId"].ToString() : string.Empty;
                _hidMenuId.Value = (null != Request.QueryString["menuId"]) ? Request.QueryString["menuId"].ToString() : string.Empty;
                /* 菜单系统类型 */
                _hidMenuSysType.Value = (null != Request.QueryString["sys"]) ? Request.QueryString["sys"].ToString() : "0";
                /* 菜单记录的创建来源类型 0:由管理员通过菜单管理功能创建 1:由模块装配功能创建 空:新增记录(根据传入的sourceType参数决定) */
                _hidRecOriType.Value = (null != Request.QueryString["recOri"]) ? Request.QueryString["recOri"].ToString() : string.Empty;
                if (!string.IsNullOrEmpty(upMenuId))
                {
                    _hidOgUpMenuId.Value = upMenuId;
                    _hidAtUpMenuId.Value = upMenuId;
                }

                //_hidSysFieldXML.Value = SessionExt.GetSysFieldXML(); tim logout 不需要session xml 2010-03-02
                _hidSysParam.Value = ConfigLib.GetAttribute(eNodeType.SYSMENUOPER);  /* 配置文件中系统参数 */

                /* add code tim 2010-01-19
                 * reason: 支持模块装配功能所做的修改 */
                MsEntryParam msEntryParam = null;
                _hidEntryParam.Value = getExtendParameter(ref msEntryParam);
                /* end */
                initializePage(msEntryParam);
                msEntryParam = null;
            }
        }

        private string getExtendParameter(ref MsEntryParam msEntryParam)
        {
            string formId = (null != Request.QueryString["formId"]) ? Request.QueryString["formId"].ToString() : string.Empty;
            string lstModeId = (null != Request.QueryString["lstModeId"]) ? Request.QueryString["lstModeId"].ToString() : string.Empty;
            string sourceType = (null != Request.QueryString["sourceType"]) ? Request.QueryString["sourceType"].ToString() : "0";
            //string retListURL = (null != Request.QueryString["retListURL"]) ? Request.QueryString["retListURL"].ToString() : string.Empty;

            msEntryParam = new MsEntryParam
            {
                formId = formId,
                lstModeId = lstModeId,
                sourceType = sourceType
                //retListURL = retListURL
            };

            string serExpr = JavaScriptConvert.SerializeObject(msEntryParam);
            return serExpr;
        }

        
        private void initializePage(MsEntryParam msEntryParam)
        {
            MenuBLL menuBLL = new MenuBLL();
            initAddNew(msEntryParam,menuBLL);

            string menuId = _hidMenuId.Value;
            MsMenu msMenu = menuBLL.GetMenuInfo(menuId);            
            initEdit(msEntryParam,msMenu);

            //btnSave.Attributes["onclick"] = "javascript:return doSave('1');";
            //btnSaveAdd.Attributes["onclick"] = "javascript:return doSave('2');";
            //btnDel.Attributes["onclick"] = "javascript:return doDelete();";

            msMenu = null;
            menuBLL.Release();
            menuBLL = null;
        }

        private void initAddNew(MsEntryParam msEntryParam, MenuBLL menuBLL)
        {
            string menuId = _hidMenuId.Value;
            string upMenuId = _hidOgUpMenuId.Value;
            string sysParam = _hidSysParam.Value;

            List<MsKeyValue> lstUpMenu = menuBLL.GetALLMenus(menuId, sysParam, true, "(无上级)");
            CtrInterface.BindDropDown(ref ddlUpMenu, lstUpMenu, true, upMenuId);
            txtMenuName.Text = string.Empty;
            rdEffect.SelectedIndex = 0;
            rdShow.SelectedIndex = 0;
            txtOrder.Text = string.Empty;
            rdLeaf.SelectedIndex = 0;
            rdPurview.SelectedIndex = 0;
            rdRange.SelectedIndex = 0;
            txtPagePath.Text = string.Empty;
            txtImgPath.Text = string.Empty;
            txtToolTip.Text = string.Empty;

            ddlUpMenu.Enabled = true;            
            txtMenuName.Enabled = true;
            txtOrder.Enabled = true;
            rdLeaf.Enabled = true;
            rdEffect.Enabled = true;
            rdShow.Enabled = true;
            rdPurview.Enabled = true;
            rdRange.Enabled = true;
            txtPagePath.Enabled = true;
            txtImgPath.Enabled = true;
            txtToolTip.Enabled = true;
            //btnSave.Visible = true;
            //btnSaveAdd.Visible = true;

            txtPagePath.Enabled = (msEntryParam.sourceType == "1") ? false : true;

            lstUpMenu = null;
        }


        private void initEdit(MsEntryParam msEntryParam, MsMenu msMenu)
        {
            if (null == msMenu) return;

            string upMenuId = msMenu.upMenuId;

            ddlUpMenu.SelectedValue = upMenuId;
            txtMenuName.Text = msMenu.menuName;
            rdEffect.SelectedValue = msMenu.effect;
            rdShow.SelectedValue = msMenu.showSign;
            txtOrder.Text = msMenu.orderNo;
            rdLeaf.SelectedValue = msMenu.isLeaf;
            rdPurview.SelectedValue = msMenu.purviewSet;
            rdRange.SelectedValue = msMenu.rangeSet;
            txtPagePath.Text = decodePath(msMenu.pagePath);
            txtImgPath.Text = msMenu.imgPath;
            txtToolTip.Text = msMenu.memo;
                        
            _hidOgUpMenuId.Value = upMenuId;
            _hidAtUpMenuId.Value = upMenuId;
            
            initialControlOper(msEntryParam);
        }

        private void initialControlOper(MsEntryParam msEntry)
        {
            //string entryParam = _hidEntryParam.Value;
            //MsEntryParam msEntry = JavaScriptConvert.DeserializeObject(entryParam, typeof(MsEntryParam)) as MsEntryParam;

            /* 业务逻辑说明:
             *    1. 如果当前菜单是系统菜单,且系统菜单可维护开关没有打开的情况下，页面不可编辑
             *    2. 如果当前菜单记录的来源类型与进入该页面的方式不相符，则页面不可编辑 tim 2010-01-20*/
            if ((_hidMenuSysType.Value == "1" && _hidSysParam.Value != "999") ||
                        _hidRecOriType.Value != msEntry.sourceType)
            {
                ddlUpMenu.Enabled = false;                
                txtMenuName.Enabled = false;
                txtOrder.Enabled = false;
                rdLeaf.Enabled = false;
                rdEffect.Enabled = false;
                rdShow.Enabled = false;
                rdPurview.Enabled = false;
                rdRange.Enabled = false;
                txtPagePath.Enabled = false;
                txtImgPath.Enabled = false;
                txtToolTip.Enabled = false;

                //btnSave.Visible = false;
                //btnSaveAdd.Visible = false;
                //btnDel.Visible = false;
            }
        }

        private string decodePath(string menuPath)
        {
            string actualPath = menuPath;
            if (string.IsNullOrEmpty(menuPath)) return actualPath;

            int questIndex = menuPath.IndexOf("?");

            string reqPage = string.Empty;
            string param = string.Empty;

            if (questIndex != -1)
            {
                reqPage = menuPath.Substring(0, questIndex + 1);
                param = menuPath.Substring(questIndex + 1);
                try
                {
                    param = Base64Lib.Decode(param);  // Decode path
                    int tabIndex = param.LastIndexOf("&TABID=");
                    if (tabIndex != -1)
                        param = param.Substring(0, tabIndex);
                    else
                    {
                        tabIndex = param.LastIndexOf("TABID=");
                        if (tabIndex != -1)
                        {
                            reqPage = reqPage.Substring(0, reqPage.Length - 1);
                            param = param.Substring(0, tabIndex);
                        }
                    }
                    actualPath = reqPage + param;
                }
                catch
                {
                    actualPath = menuPath;
                }
            }

            return actualPath;
        }

        protected void lnbCallBack_Click(object sender, EventArgs e)
        {
            string entryParam = _hidEntryParam.Value;
            MsEntryParam msEntryParam = JavaScriptConvert.DeserializeObject(entryParam, typeof(MsEntryParam)) as MsEntryParam;
            initializePage(msEntryParam);
            msEntryParam = null;
        }


        #region ajax methods
        /// <summary>
        /// 保存菜单信息
        /// </summary>
        /// <param name="jsonExpr"></param>
        /// <param name="ctrlType"></param>
        /// <param name="checkType"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string SaveMenu(string jsonExpr, string ctrlType, string checkType)
        {
            MenuBLL menuBLL = new MenuBLL();
            MsMenu msMenu = JavaScriptConvert.DeserializeObject(jsonExpr, typeof(MsMenu)) as MsMenu;
            eTrueFalse isCheck = (eTrueFalse)Enum.Parse(typeof(eTrueFalse), checkType);
            List<string> lstResult = menuBLL.Save(msMenu,isCheck);
            int itemCount = lstResult.Count;
            var newObj = new {
                OPERTYPE = "MENUSAVE",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1],
                MENUNAME = (itemCount > 2) ? lstResult[2] : string.Empty,
                UPMENUID = (itemCount > 2) ? lstResult[3] : string.Empty,
                HASCHILD = (itemCount > 2) ? lstResult[4] : string.Empty,
                SYSSIGN = (itemCount > 2) ? lstResult[5] : string.Empty,
                LAY = (itemCount > 2) ? lstResult[6] : string.Empty,
                ORITYPE = (itemCount > 2) ? lstResult[7] : string.Empty,
                CTRLTYPE = ctrlType
            };

            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            msMenu = null;
            lstResult = null;
            menuBLL.Release();
            menuBLL = null;
            return serExpr;
        }

        

        /// <summary>
        /// 菜单删除
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeleteMenu(string menuId)
        {
            MenuBLL menuBLL = new MenuBLL();
            List<string> lstResult = menuBLL.Delete(menuId);
            var newObj = new {
                OPERTYPE = "MENUDELETE",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1]
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            lstResult = null;
            menuBLL.Release();
            menuBLL = null;
            return serExpr;
        }



        #endregion
        
    }
}
